home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3580 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Recursive function -> how do you exit one?
  5. Date: 29 Jan 1996 12:42:08 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4ejbf0INN2b3@keats.ugrad.cs.ubc.ca>
  8. References: <4eh1g8$aba@pulp.ucs.ualberta.ca>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4eh1g8$aba@pulp.ucs.ualberta.ca>,
  12. Jacob Bukczynski <jbukczyn@gpu.srv.ualberta.ca> wrote:
  13. >I'm using a recursive function to search for files. I need
  14. >it to quit when a user presses Esc.
  15. >
  16. >Using 
  17. >
  18. >return;
  19. >
  20. >doesn't seem to work, the funtion runs again. ( I tested
  21. >this by putting a printf statement in front of the return -
  22. >it got printed over and over again. )
  23. >
  24. >Is there a special way of exiting a recursive function?
  25.  
  26. The Elegant Way:
  27. ----------------
  28.  
  29. Make whether or not the user wants to exit a condition of the recursion.
  30. Then do something like "return USERQUIT; "  if the user wants to quit.
  31. The next higher level of recursion will detect this return value, and chain it
  32. by also doing "return USERQUIT; " all the way to the top level. Whoever called
  33. the recursive function will know that it had been aborted.
  34.  
  35.  
  36. The Real Way:
  37. -------------
  38.  
  39. Use setjmp() to mark the stack frame before entering the recursive search.
  40. When the user quits, use longjmp() to throw away all the stack frames in one
  41. fell swoop and return to the caller. You have to be careful, since this can
  42. clobber the caller's local variables, which are typically assigned to
  43. registers. Declare the caller's locals as "volatile".
  44.  
  45. >Please reply at jbukczyn@gpu.srv.ualberta.ca
  46.  
  47. CC sent.
  48. -- 
  49.  
  50.